#include <bits/stdc++.h> 
#define int long long
#define PII pair <int, int>
using namespace std;
const int N = 4e6 + 5;
int n, idx, ans = 1e18;
int vis[N];
vector <int> a[N];
multiset <int> st;
struct Node { int x, y, num; } cpy[N];
bool cmp(Node A, Node B) {
	if(A.num != B.num) return A.num > B.num;
	if(A.x != B.x) return A.x > B.x;
	return A.y > B.y;
}
inline int read() {
    char c = getchar();
    bool sign = (c == '-');
    while (c < '0' || c > '9') c = getchar(), sign |= (c == '-');
    int x = 0;
    while ('0' <= c && c <= '9') x = (x << 1) + (x << 3) + (c & 15), c = getchar();
    return sign ? (~x + 1) : x;
}
signed main() {
	freopen("contest.in", "r", stdin);
	freopen("contest.out", "w", stdout);
	ios :: sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	n = read();
	if(n == 1) { cout << 0; return 0; }
	for(int i = 1, k; i <= n; i ++ ) {
		k = read();
		for(int j = 0; j < k; j ++ ) a[i].push_back(read());
		sort(a[i].begin(), a[i].end());
		for(int j = 0; j < k; j ++ ) cpy[ ++ idx] = {i, j, a[i][j]};
	}
	sort(cpy + 1, cpy + idx + 1, cmp);
	for(int i = 1, cnt = 0; i <= idx; i ++ ) {
		int x = cpy[i].x, y = cpy[i].y;
		cnt += !vis[x];
		vis[x] = 1;
		if(a[x].size() > y + 1) st.erase(st.find(a[x][y + 1]));
		if(cnt == n) ans = min(ans, *st.rbegin() - cpy[i].num);
		st.insert(cpy[i].num);
	}
	cout << ans;
	return 0;
}

/*

4
9 -5 -9 2 8 5 4 3 3 8
2 10 8
1 -7
3 1 6 10

1
2 1 2

*/
